trigger OORBookingUpdates on Booking_Dates__c (after Insert,after Update) {

       //Trigger Name: OORBookingUpdates
       //Trigger Purpose : Trigger to Update Booking Details on Opportunity and Split Order
       //Project Name: OOR Phase 2
       //Author: Accenture IDC
   
       List<Opportunity> Opptys= New List<Opportunity>();// List for Querying all OOR Opportunity with Submitted Date not equals null
       List<Opportunity> OpptystoUpdate= New List<Opportunity>();//List to store the Updated Opportunites
       List<Booking_Dates__c> books  = new List<Booking_Dates__c>();
       ID SFAOORLeadOrderAdvertiserRecordTypeID = RecordTypeHelper.GetRecordTypeID('Opportunity', 'SFA-OOR Lead Order');
       ID SFAOORRenewalecordTypeID = RecordTypeHelper.GetRecordTypeID('Opportunity', 'SFA-OOR Renewals');
       
       Map<String,Date> bookingdatesmap = New Map<String,Date>(); //Map to Store all the Bookind Date Object's Booking Month and Booking Closing Date
       
       String month; // Variable for Storing Month Value of the Submitted Date
       Date myDate= Date.Today();
       String year = String.ValueOf(myDate.Year());
   
              
            books = [select Id,Booking_Closing_Year__c,Booking_Closing_Month__c,Booking_Closing_Date__c from Booking_Dates__c where Booking_Closing_Year__c =: year];
            
                for(Booking_Dates__c  bookingDate:books){
                    bookingdatesmap.put(bookingDate.Booking_Closing_Month__c,bookingDate.Booking_Closing_Date__c);
                }
                Opptys =[Select Id,Order_Completion_Date__c,Order_Date_SOLO__c,SOLO_Submitted_Date__c,Booking_Closing_Date__c from Opportunity Where  (StageName!=:'Canceled' OR StageName!=:'Invalid') AND  SOLO_Submitted_Date__c != NULL AND(RecordTypeId=:SFAOORLeadOrderAdvertiserRecordTypeID OR RecordTypeId =:SFAOORRenewalecordTypeID)];
                
                     for(Opportunity Opty : Opptys){
                          Date submissiondate=Opty.SOLO_Submitted_Date__c;
                              
                             for(integer j=submissiondate.Month();j<bookingdatesmap.size()+1;j++){ 
                                 month =String.valueOf(j);
                                 if(Opty.Order_Completion_Date__c != NULL){
                                 
                                         if(((Opty.Order_Completion_Date__c==bookingdatesmap.get(month ))||(Opty.Order_Completion_Date__c<bookingdatesmap.get(month)))){
                                             Opty.Booking_Closing_Date__c = bookingdatesmap.get(month);
                                             integer noOfDays = Date.daysInMonth(myDate.year(), Integer.ValueOf(month));
                                             Opty.Booked_Date__c = Date.newInstance(myDate.year(),Integer.ValueOf(month), noOfDays); 
                                             OpptystoUpdate.add(Opty);
                                         }
                                                                                  
                                    }     
                                
                               }               
                                                                    
                        } 
                 
                 
                 if(OpptystoUpdate.Size()>0)
                   {
                     try{
                         Update OpptystoUpdate; 
                         }
                         catch(DMLException exe){
                         GeneralConstants.mailAnyExceptionToMe('OORBookingUpdates ','Trigger on Booking Dates',exe);
                        }
                    }
}